﻿using System;
using System.Text;

namespace WS.FlowAuth.Converter
{
    public static class Converter
    {
        public static string ConvertOpCode(string opcode)
        {
            if (opcode == "A0")
            {
                return @"修改累积量";
            }
            if (opcode == "A1")
            {
                return @"设置修正系数";
            }
            return opcode;
        }
        public static string ParseOpData(string opcode, byte[] opdata)
        {
            if (opcode == "A0" && opdata.Length == 16)
            {
                double from = BitConverter.ToDouble(opdata, 0);
                double to = BitConverter.ToDouble(opdata, 8);
                return @"累积量从<span style='color:green'>"
                  + $"{from:F2}</span>修改为<span style='color:red'>{to:F2}</span>";
            }
            if (opcode == "A1" && opdata.Length == 82)
            {
                ushort count = BitConverter.ToUInt16(opdata, 0);
                if (count > 0 && count <= 10)
                {
                    var builder = new StringBuilder();
                    builder.Append(
                      @"<table><tr><td>&nbsp;</td><td>流量</td><td>修正值</td></tr>");
                    for (var i = 0; i < count; ++i)
                    {
                        var flow = BitConverter.ToSingle(opdata, i * 8 + 2);
                        var fix = BitConverter.ToSingle(opdata, i * 8 + 6);
                        builder.Append(
                          $"<tr><td>{i + 1}</td><td>{flow:F2}</td><td>{fix:F2}</td></tr>");
                    }
                    builder.Append("</table>");

                    return builder.ToString();
                }
            }
            return BitConverter.ToString(opdata).Replace("-", "");
        }
    }
}
